//
//  AIAddressViewController.m
//  aixianfeng
//
//  Created by 李江波 on 2016/12/22.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "AIAddressViewController.h"

#import "AIAddressModel.h"
#import "AIAddAddressController.h"
#import "AIAddressCell.h"
#import "AIBasicViewController.h"

#define AddressPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"address.plist"]

static NSString *cellID = @"AIAddressCell";
@interface AIAddressViewController ()<UITableViewDelegate ,UITableViewDataSource>

@property(nonatomic ,weak) UIView *addAddressView;
@property(nonatomic ,strong) UITableView *tableView;

@property(nonatomic ,strong) NSMutableArray<AIAddressModel *>*addressModels;

@property(nonatomic ,strong) AIAddAddressController *VC;
@property(nonatomic ,strong) UIView *noAddressView;

@end

@implementation AIAddressViewController

-(UIView *)noAddressView{

    if (nil == _noAddressView) {
        
        _noAddressView = [[UIView alloc]init];
        _noAddressView.backgroundColor = [UIColor whiteColor];
        [self.tableView addSubview:_noAddressView];
        [_noAddressView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view);
            make.bottom.equalTo(self.view).offset(-49);
        }];
        
        UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"v2_address_empty"]];
        [_noAddressView addSubview:imageV];
        [imageV sizeToFit];
        UILabel *lable = [UILabel cz_labelWithText:@"姐姐，没地址啊😭" fontSize:18 color:[UIColor blackColor]];
        [_noAddressView addSubview:lable];
        
        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.center.equalTo(_noAddressView);
        }];
        [lable mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.centerX.equalTo(self.view);
            make.top.equalTo(imageV.mas_bottom).offset(15);
        }];
        
    }
    return _noAddressView;
}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.barTintColor = [UIColor cz_colorWithHex:0xFDD432];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    self.title = @"我的收获地址";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addAddress];
    [self setupTableview];
    [self loadData];

}
-(void)addAddress{

    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.bottom.equalTo(self.view);
        make.height.offset(49);
    }];
    
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:@"+新增地址" forState:UIControlStateNormal];
    btn.layer.cornerRadius = 5;
    btn.layer.masksToBounds = YES;
    btn.backgroundColor = [UIColor cz_colorWithHex:0xFDD432];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.tag = 100;
    [btn addTarget:self action:@selector(clickAddAddress:) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.center.equalTo(view);
        make.height.offset(35);
        make.width.offset(250);
    }];
    
    _addAddressView = view;
}

-(void) setupTableview{

    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.rowHeight = 80;
    
    [_tableView registerNib:[UINib nibWithNibName:@"AIAddressCell" bundle:nil] forCellReuseIdentifier:cellID];
    
    [self.view insertSubview:_tableView belowSubview:_addAddressView];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
}

-(void)loadData{
////读取数据,结档
    
    self.addressModels = [NSKeyedUnarchiver unarchiveObjectWithFile:AddressPath];
    
    if (self.addressModels == nil) {
        
        self.addressModels = [NSMutableArray array];
        
    }
    if (self.addressModels.count == 0) {
        
        self.noAddressView.hidden = NO;
    }else{
    
        self.noAddressView.hidden = YES;
    }
}

#pragma mark : - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.addressModels.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    AIAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.model = self.addressModels[indexPath.row];
    cell.selected = UITableViewCellSelectionStyleNone;
    cell.editClickBlock = ^(UIButton *btn){
        btn.tag = indexPath.row;
        [self clickAddAddress:btn];
    };
    return cell;
}


#pragma mark : - 点击增加地址按钮

-(void)clickAddAddress:(UIButton *)btn{

    _VC = [[UIStoryboard storyboardWithName:@"AIAddAddressController" bundle:nil] instantiateInitialViewController];
    __weak typeof(self) weakself = self;
    _VC.saveBlock = ^(AIAddressModel *model){
    
        if (![weakself.addressModels containsObject:model]) {
            
            [weakself.addressModels insertObject:model atIndex:0];
        }
        [weakself.tableView reloadData];
        [weakself saveAddress];
    };
    
    if (btn.tag != 100) {
        
        _VC.model = self.addressModels[btn.tag];
    }
    
    [self.navigationController pushViewController:_VC animated:YES];
}

-(void)saveAddress{

    [NSKeyedArchiver archiveRootObject:_addressModels toFile:AddressPath];
    if (self.addressModels.count == 0) {
        
        self.noAddressView.hidden = NO;
    }else{
    
        self.noAddressView.hidden = YES;
    }
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    [self.addressModels removeObjectAtIndex:indexPath.row];
    [self.tableView reloadData];
    [self saveAddress];
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"删除";
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    if (self.changeAddressBlock) {
        
        self.changeAddressBlock(self.addressModels[indexPath.row].area);
    }
    
}









@end
